package com.wyk.service;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.stereotype.Service;

import com.wyk.entity.RegisterUser;
import com.wyk.session.AttributionKeys;

@Service
public class RegisterUserService {
	
	public RegisterUser findOne(){
		RegisterUser registerUser = new RegisterUser();
		registerUser.setPassword("123456");
		registerUser.setUserName("helloJava");
		return registerUser;
	}
	
	public UsernamePasswordToken transform(RegisterUser registerUser){
		UsernamePasswordToken token = new UsernamePasswordToken(registerUser.getUserName(),registerUser.getPassword());
		return token;
	}
	
	/**
	 * 登陆
	 * @param token
	 */
	public void login(UsernamePasswordToken token){
		SecurityUtils
			.getSubject()
				.login(token);
	}
	
	public void login(){
		UsernamePasswordToken token = new UsernamePasswordToken("name", "123456");
		SecurityUtils.getSubject().login(token);
	}
	
	/**
	 * 将用户存入session
	 * @param registerUser
	 */
	public void setUserSession(RegisterUser registerUser){
		SecurityUtils.getSubject().getSession().setAttribute(AttributionKeys.USER_SESSION_KEY, registerUser);
		System.out.println(getSessionId());
	}
	/**
	 * 从session里获取用户
	 * @return
	 */
	public RegisterUser getUserSession(){
		return (RegisterUser) SecurityUtils.getSubject().getSession().getAttribute(AttributionKeys.USER_SESSION_KEY);
	}
	
	public String getSessionId(){
		return SecurityUtils.getSubject().getSession().getId().toString();
	}
}
